﻿<?php
    
    //优化初始化
    include('init.php');
 
	
	 
	 //获取地址栏的p参数中所存储的页码数
	 $p = $_GET['p'];
	 
	  //设需要数据条的值
     $page_num = 10;
	 
	 //设需要固定页码的值
	 $page_sum = 5;
	 
	 //计算limit第一个参数的值实现翻页那么这个值有个专业的名词叫做变异量
	 //求变异量的数值
	 $offset = ($p-1)* $page_num;
	 
	 
	 //求表的总记录数
	 $sql = "select count(*) count from `news`";
	 
	 $result = mysqli_query($link, $sql);
	 
	 //求数据有多少条
	 $total_num = mysqli_fetch_assoc($result)['count'];
	 
	 //print_r($total_num);
	 
	 //求总页数多少
	 //ceil：向上取整
	 $tatal_page = ceil($total_num/$page_num);
	 
	 
	 //页码前后偏移
	 $offset_page = floor($page_sum/2);
	 
	 //求循环的起始值
	    $star = $p-$offset_page;
	 //求循环的结束值
	    $end = $p+$offset_page;
	 
	 
	 //判断如果起始值小于0时将其强制变成1，结束值等于固定页码的值
	 if($star<=0){
	 	$star = 1;
		$end =$page_sum;
	 }
	 
	 //判断如果结束值大于总页码值时则将其变成总页码数；起始值=总页码值-固定页码值+1；
	 if($end>$tatal_page){
	 	$end = $tatal_page;
	 	$star = $tatal_page-$page_sum+1;
	 	
	 }
	 
	 //判断如果结束值的总页码数小于结束值的固定页码时，起始值等于1，结束值等于总页码值
	 if($tatal_page<$page_sum){
	 	$star =1;
	 	$end = $tatal_page;
	 }
	 
	 //动态生成翻页页码
	$pages = '';
	 
	$pages .= '<li class="prev disabled"><a href="#">首页</a></li>';
	$pages .= '<li class="prev disabled"><a href="#">上一页</a></li>';
		 for($i=$star;$i<=$end;$i++) {
	$pages .= '<li><a href="news_list.php?p='.$i.'">'.$i.'</a></li>';
		 }
	$pages .= '<li class="next"><a href="#">下一页</a></li>';
	$pages .= '<li class="list"><a href="#">尾页</a></li>';
	 
	 
	 
	  //编写查询语句  倒序
	 $sql ="select * from `news` order by id desc limit $offset,$page_num";
	 
	 //编写查询语句 删除没删除的数据
	 //$sql = "slect * from `news` where enbale=0 order by id besc"
	 
	 //将mysqli_query执行的查询语句返回集存储到$result
	 $result = mysqli_query($link, $sql);
	 
	 
	 //使用将结果集中的数据逐一出来存储在数组中
     //	 print_r($data);die;
     $data= array();
     while($arr = mysqli_fetch_assoc($result)){
     	 $data[] = $arr;
     }
//   print_r($data);die;
    //加载新闻添加页视图
    include ('view/layout.html');

?>
